package com.manager.devicemanager.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.manager.devicemanager.common.DataSourceNameEnum;
import com.manager.devicemanager.common.Result;
import com.manager.devicemanager.entity.Component1;
import com.manager.devicemanager.entity.PDTestcomponentStatus1;
import com.manager.devicemanager.entity.ComponentRunStatus1;
import com.manager.devicemanager.service.IComponentRunStatus1Service;
import com.manager.devicemanager.util.StringDealUtils;
import com.manager.devicemanager.vo.ComponentStatusRequestVO;
import com.manager.devicemanager.service.IDpcomponentStatus1Service;
import com.manager.devicemanager.vo.DPComponentStatusVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import com.manager.devicemanager.vo.PageVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wpf
 * @since 2025-06-30
 */
@Tag(name = "DpcomponentStatus1Controller", description = " 相关接口")
@RestController
@RequestMapping("/dpcomponentboardstatus")
public class DpcomponentStatus1Controller {

    @Autowired
    private IDpcomponentStatus1Service iDpcomponentStatus1Service;
    @Autowired
    private IComponentRunStatus1Service iComponentRunStatus1Service;

    @Operation(summary = "新增")
    @PostMapping("/save")
    public Result<PDTestcomponentStatus1> save(@Parameter(description = "DpcomponentStatus1 对象") @RequestBody PDTestcomponentStatus1 PDTestcomponentStatus1) {
        iDpcomponentStatus1Service.save(PDTestcomponentStatus1);
        return Result.success(PDTestcomponentStatus1);
    }


    @Operation(summary = "条件查询带分页")
    @PostMapping("/page")
    public Result<IPage<PDTestcomponentStatus1>> page(@Parameter(description = "查询条件对象") @RequestBody PageVO<PDTestcomponentStatus1> dpcomponentStatus1Query) {
        int pageNum = dpcomponentStatus1Query.getPageNum();
        int pageSize = dpcomponentStatus1Query.getPageSize();
        PDTestcomponentStatus1 PDTestcomponentStatus1 = dpcomponentStatus1Query.getCondition();
        QueryWrapper<PDTestcomponentStatus1> wrapper = new QueryWrapper<>(PDTestcomponentStatus1);
        //if (StringUtils.hasText(dpcomponentStatus1.getName())) {
        //  wrapper.like("name", dpcomponentStatus1.getName());
        //}
        IPage<PDTestcomponentStatus1> dpcomponentStatus1List = iDpcomponentStatus1Service.page(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(dpcomponentStatus1List);
    }

    @Operation(summary = "条件查询")
    @PostMapping("/list")
    public Result<List<PDTestcomponentStatus1>> list(@Parameter(description = "DpcomponentStatus1 查询条件对象") @RequestBody PDTestcomponentStatus1 PDTestcomponentStatus1) {
        List<PDTestcomponentStatus1> dpcomponentStatus1List = iDpcomponentStatus1Service.list(new QueryWrapper<>(PDTestcomponentStatus1));
        return Result.success(dpcomponentStatus1List);
    }

    @Operation(summary = "获取最新的一条数据")
    @PostMapping("/getLatestData")
    public Result<JSONArray> getLatestData(@Parameter(description = "DpcomponentStatus1 查询条件对象") @RequestBody ComponentStatusRequestVO componentStatusRequestVO) {
        try {
            PDTestcomponentStatus1 latestData = iDpcomponentStatus1Service.selectComponentStatusByTestIdAndBoardid(DataSourceNameEnum.PDBOARDATA.getDataSourceName(), componentStatusRequestVO.getTestid(), componentStatusRequestVO.getBoardid());
            if (latestData == null) {
                return Result.error("没有数据");
            }
            ComponentRunStatus1 lastData = iComponentRunStatus1Service.getLastData(DataSourceNameEnum.PDBOARDCONFIG.getDataSourceName());
            JSONArray jsonArray = new JSONArray();

            String[] statusArray = null;
            if (lastData != null) {

                String methodName = "getS" + componentStatusRequestVO.getBoardid();
                String s1 = null;
                try {
                    java.lang.reflect.Method method = ComponentRunStatus1.class.getMethod(methodName);
                    s1 = (String) method.invoke(lastData);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (s1 != null) {
                    s1 = StringDealUtils.dealDPComponentString(s1);
                    statusArray = s1.split(":");
                }
            }

            for (int i = 1; i <= 60; i++) {
                String methodName = "getC" + i;
                String dl = "";
                try {
                    java.lang.reflect.Method method = PDTestcomponentStatus1.class.getMethod(methodName);
                    dl = (String) method.invoke(latestData);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("status", statusArray != null && statusArray.length >= i ? statusArray[i - 1] : "");
                jsonObject.put("dl", dl);
                jsonObject.put("boardid", i);
                jsonArray.add(jsonObject);
            }

            return Result.success(jsonArray);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/getOneComponentLine")
    public Result<List<DPComponentStatusVO>> getOneComponentLine(@Parameter(description = "DpcomponentStatus1 查询条件对象") @RequestBody ComponentStatusRequestVO componentStatusRequestVO) {
        try {
            List<DPComponentStatusVO> component1s = iDpcomponentStatus1Service.getOneComponentLine(DataSourceNameEnum.PDBOARDATA.getDataSourceName()
                    + componentStatusRequestVO.getDatasourceindex(), componentStatusRequestVO);
            return Result.success(component1s);
        } catch (Exception e) {
            e.printStackTrace();
            return  Result.error("获取数据出错");
        }

    }
}
